﻿#include "RecordTable.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QHeaderView>

#pragma execution_character_set("utf-8")

RecordTable::RecordTable(QWidget *parent)
    : QWidget{parent}
{
    setupUi();
    initConnection();
}

void RecordTable::setupUi()
{
    tblView = new QTableView();
    model = new RecordTableModel();
    tblView->setModel(model);
    tblView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    tblView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    tblView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    tblView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    headerMenu = new QMenu(tblView->horizontalHeader());
    actHideItem = new QAction("隐藏");
    actAscend = new QAction("升序");
    actDescend = new QAction("降序");
    actReshowItem = new QAction("重置");

    headerMenu->addAction(actHideItem);
    headerMenu->addAction(actAscend);
    headerMenu->addAction(actDescend);
    headerMenu->addAction(actReshowItem);

    tbtnPageFirst = new QToolButton();
    tbtnPageFirst->setToolButtonStyle(Qt::ToolButtonIconOnly);
    tbtnPageFirst->setIcon(QPixmap(":/resource/page_first.png"));
    tbtnPageFirst->setToolTip(tr("首页"));

    tbtnPageLast = new QToolButton();
    tbtnPageLast->setToolButtonStyle(Qt::ToolButtonIconOnly);
    tbtnPageLast->setIcon(QPixmap(":/resource/page_last.png"));
    tbtnPageLast->setToolTip(tr("末页"));

    tbtnPageDown = new QToolButton();
    tbtnPageDown->setToolButtonStyle(Qt::ToolButtonIconOnly);
    tbtnPageDown->setIcon(QPixmap(":/resource/page_down.png"));
    tbtnPageDown->setToolTip(tr("下页"));

    tbtnPageUp = new QToolButton();
    tbtnPageUp->setToolButtonStyle(Qt::ToolButtonIconOnly);
    tbtnPageUp->setIcon(QPixmap(":/resource/page_up.png"));
    tbtnPageUp->setToolTip(tr("上页"));

    tbtnPageJump = new QToolButton();
    tbtnPageJump->setToolButtonStyle(Qt::ToolButtonIconOnly);
    tbtnPageJump->setIcon(QPixmap(":/resource/jump_to.png"));
    tbtnPageJump->setToolTip(tr("跳到"));

    spboxPageNow = new QSpinBox();
    spboxPageNow->setMaximumWidth(80);
    spboxPageNow->setValue(0);

    lblPageAll = new QLabel("/0");

    QVBoxLayout* vLayout = new QVBoxLayout();
    this->setLayout(vLayout);
    vLayout->addWidget(tblView);

    QHBoxLayout *hLayout = new QHBoxLayout();
    hLayout->addStretch();
    hLayout->addWidget(tbtnPageFirst);
    hLayout->addWidget(tbtnPageUp);
    hLayout->addWidget(spboxPageNow);
    hLayout->addWidget(lblPageAll);
    hLayout->addWidget(tbtnPageDown);
    hLayout->addWidget(tbtnPageLast);
    hLayout->addWidget(tbtnPageJump);
    hLayout->addStretch();
    vLayout->addLayout(hLayout);
}

void RecordTable::initConnection()
{
    connect(tbtnPageFirst,&QToolButton::clicked, this, [&](){
        model->firstPage();
        spboxPageNow->setValue(model->currentPage());
    });
    connect(tbtnPageLast,&QToolButton::clicked, this, [&](){
        model->lastPage();
        spboxPageNow->setValue(model->currentPage());
    });
    connect(tbtnPageDown,&QToolButton::clicked, this, [&](){
        model->nextPage();
        spboxPageNow->setValue(model->currentPage());
    });
    connect(tbtnPageUp,&QToolButton::clicked, this, [&](){
        model->prevPage();
        spboxPageNow->setValue(model->currentPage());
    });
    connect(tbtnPageJump,&QToolButton::clicked, this, [&](){
        model->goToPage(std::min(spboxPageNow->value(), lblPageAll->text().toInt()));
        spboxPageNow->setValue(model->currentPage());
    });
    connect(model, &QAbstractItemModel::dataChanged, this, [&](){
        lblPageAll->setText(QString("/%1").arg(model->totalPages()));
    });

}

